/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.template.Jalopy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.hyts.guava.object.Objects;
import com.hyts.jlogger.factory.LoggerFactory;

import de.hunsicker.jalopy.Jalopy;
import de.hunsicker.jalopy.language.antlr.JavaNode;
import de.hunsicker.jalopy.storage.History.Policy;

/** 
 * @author LiBo/Alex
 * @see de.hunsicker.jalopy.Jalopy
 * @since V1.0
 * @version V1.0 
 */
public class JalopyFormat {
    
    /**  
     * <p>日志输出器}</p> 
     * @fieldName logger
     * @fieldType Logger
     */ 
    private static Logger logger = LoggerFactory.getLogger(JalopyFormat.class);
    
    /**  
     * <p>coreObject</p> 
     * @fieldName javaFormat
     * @fieldType Jalopy
     */ 
    private static final Jalopy coreObject = new Jalopy();
    
    /**  
     * <p>{javaFormat</p> 
     * @fieldName javaFormat
     * @fieldType JavaFormat
     */ 
    private static final JalopyFormat javaFormat = new JalopyFormat();
    
    /**  
     * <p>编码格式字符集</p> 
     * @fieldName defaultEncoding
     * @fieldType String
     */ 
    private static volatile String defaultEncoding = "UTF-8";
    
    
    
    /**  
     * @constructor：JavaFormat    
     */ 
    private JalopyFormat() {
        super();
    }
    
    /**  
     * <b>getInstance</b>
     * @return
     * @exception
     */ 
    public static final JalopyFormat getInstance() {
        return javaFormat;
    }
    
    /*************************************输入对象属性**********************************************/
    
    /**  
     * @name coreobject's getter method
     * @param none
     * @return coreobject
     */
    public static Jalopy getCoreobject() {
        return coreObject;
    }

    /**  
     * <b>encoding</b>
     * @param encoding
     * @return
     * @exception
     */ 
    public JalopyFormat encoding(String encoding) {
        defaultEncoding = encoding;
        return this;
    }
    
    /**  
     * <b>build</b>
     * @return
     * @exception
     */ 
    private void create() {
        coreObject.setEncoding(defaultEncoding);
        coreObject.setBackup(false);
        coreObject.setForce(true);
        coreObject.setHistoryPolicy(Policy.COMMENT);
        coreObject.setInspect(true);
        return ;
    }
    
    /*************************************输入数据对象**********************************************/
    
    /**  
     * <b>accept</b>
     * @param codeFile
     * @return
     * @exception
     */ 
    public JalopyFormat from(File codeFile) {
        create();
        try {
            coreObject.setInput(codeFile);
        } catch (FileNotFoundException e) {
            logger.log(Level.SEVERE,"accept文件失败！",e);
        }
        return this;
    }
    
    /**  
     * @param codeStr
     * @return
     * @exception
     */ 
    public JalopyFormat from(String codeStr) {
        create();
        coreObject.setInput(codeStr,String.valueOf(Objects.getHashCode()).concat(".java"));
        return this;
    }
    
    /**  
     * @param codeDataStream
     * @return
     * @exception
     */ 
    public JalopyFormat from(InputStream codeDataStream) {
        create();
        coreObject.setInput(codeDataStream, String.valueOf(Objects.getHashCode()).concat(".java"));
        return this;
    }
    
    
    /**  
     * @param reader
     * @return
     * @exception
     */ 
    public JalopyFormat from(Reader reader) {
        create();
        coreObject.setInput(reader,  String.valueOf(Objects.getHashCode()).concat(".java"));
        return this;
    }
    
    /*************************************输出数据对象**********************************************/
    
    /**  
     * <b>to</b>
     * @param codeFile
     * @return
     * @exception
     */ 
    public JalopyFormat to(File codeFile) {
        try {
            coreObject.setOutput(codeFile);
        } catch (Exception e) {
            logger.log(Level.SEVERE,"out文件失败！",e);
        }
        return this;
    }
    
    /** 
     * <b>to</b>
     * @param codeStr
     * @return
     * @exception
     */ 
    public JalopyFormat to(StringBuffer sb) {
        create();
        coreObject.setOutput(sb);
        return this;
    }
    
    /**  
     * <b>to</b>
     * @param codeDataStream
     * @return
     * @exception
     */ 
    public JalopyFormat to(Writer codeDataStream) {
        create();
        coreObject.setOutput(codeDataStream);
        return this;
    }
    
   
    /*************************************调整数据对象**********************************************/
    
    
    
    public JavaNode parseJava() {
       return coreObject.parse();
    }
    
    /**  
     * @param clear
     * @return
     * @exception
     */ 
    public boolean format(boolean clear) {
        return coreObject.format(clear);
    }
    
    /**  
     * @return
     * @exception
     */ 
    public boolean format() {
        return coreObject.format();
    }
    
    /**  
     * @param back
     * @return
     * @exception
     */ 
    public JalopyFormat back(String back) {
        coreObject.setBackupDirectory(back);
        return this;
    }
    
    /**  
     * @return
     * @exception
     */ 
    public boolean codeFormatter(String codeFile) {
        File codeFileJava = new File(codeFile);
        return JalopyFormat.getInstance().from(codeFileJava).to(codeFileJava).format();
    }
    
    // 格式化java代码
    public static void main(String args[]) throws FileNotFoundException {
     /*   StringBuffer output = new StringBuffer();
        Jalopy j = new Jalopy();
        j.setEncoding("UTF-8");
        j.setInput("public class JalopyTest {"
                + "public void foo() {}  int getI(){return 1;}}", "A.java"); // 第二个参数随便填个java类名
        j.setOutput(output);
        System.out.println(output);*/
        String str = "public class JalopyTest {"
        + "public void foo() {}  int getI(){return 1;}}";
        StringBuffer out = new StringBuffer();
        //File inFile = new File("D:\\other_space\\coffice-toolbox\\coffice-toolbox-utils\\coffice-toolbox-template\\src\\main\\java\\com\\hyts\\template\\common\\AbstractFileWriter.java");
        File outFile = new File("/Users/alex/Documents/我的项目/osfd/coffice-toolbox/coffice-toolbox-monitor/coffice-toolbox-monitor-remotec/src/main/java/com/hyts/remotec/RebotHandler.java");
        JalopyFormat.getInstance().from(outFile).to(outFile).format();
    }
    
}
